package com.easygem.front.service.imp;

import com.easygem.business.service.base.AbstractServiceImpl;
import com.easygem.common.error.SystemCodeEnum;
import com.easygem.common.error.SystemException;
import com.easygem.common.model.front.Blacklist;
import com.easygem.common.vo.front.BlacklistVo;
import com.easygem.common.vo.system.PageVO;
import com.easygem.front.converter.BlacklistConverter;
import com.easygem.front.mapper.BlackListMapper;
import com.easygem.front.mapper.DepartmentMapper;
import com.easygem.front.service.BlackListService;
import com.easygem.front.service.ResourceInfoService;
import com.easygem.system.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 肖智
 * @version 1.0
 * @Description TODO
 * @date 2025/1/2
 */
@Service
public class BlackListServiceImpl extends AbstractServiceImpl<BlackListMapper, Blacklist, Integer> implements BlackListService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private BlackListMapper blacklistMapper;
    @Autowired
    private ResourceInfoService resourceInfoService;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Integer addBlackList(Blacklist blacklist) {
        int isBlacklist = resourceInfoService.isBlacklist(blacklist.getClientPhone());
        int result = 0;
        if (isBlacklist <= 0) {
            result = blacklistMapper.addBlackList(blacklist);
        }
        return result;
    }

    @Override
    public PageVO<Blacklist> findList(Integer pageNum, Integer pageSize, BlacklistVo blacklistVo) {
        Blacklist blacklist = new Blacklist();
        BeanUtils.copyProperties(blacklistVo, blacklist);
        PageHelper.startPage(pageNum, pageSize);
        List<Blacklist> blacklistList = blacklistMapper.getBlackListByDepId_List(blacklist);
        PageInfo<Blacklist> info = new PageInfo<>(blacklistList);
        return new PageVO<>(info.getTotal(), blacklistList);
    }

    /**
     * 添加黑名单
     *
     * @param blacklistVo 对象
     */
    @Override
    public void add(BlacklistVo blacklistVo) {
        Blacklist blacklist = new Blacklist();
        blacklistVo.setCreateTime(String.valueOf(new Timestamp(System.currentTimeMillis())));
        //调用根据账号得到admin的id的通用方法
        blacklistVo.setUserId(userMapper.getUserIdByAccountName (blacklistVo.getAccountName()));
        blacklist.setRichClass(1);
        BeanUtils.copyProperties(blacklistVo, blacklist);
        blacklistMapper.insert(blacklist);
    }

    /**
     * 删除黑名单
     * @param id
     */
    @Override
    public void delete(String id) throws SystemException {
        Blacklist blacklist = blacklistMapper.selectByPrimaryKey(id);
        if (blacklist == null) {
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR, "要移除的黑名单不存在");
        }
        blacklistMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id集合多个删除，批量删除
     *
     * @param blacklistIds id集合
     * @return
     */
    @Override
    public int delByBlacklistIds(String[] blacklistIds) {
        return blacklistMapper.delByBlacklistIds(blacklistIds);
    }

    /**
     * 通过条件查出黑名单数据，用于导出
     *
     * @return
     */
    @Override
    public List<Blacklist> findByBlacklistAll(BlacklistVo blacklistVo) {
        Blacklist blacklist = new Blacklist();
        BeanUtils.copyProperties(blacklistVo, blacklist);
        List<Blacklist> blacklistList = blacklistMapper.getBlackListByDepId_List(blacklist);
        return blacklistList;
    }

    @Override
    public int delBlacklistIds(BlacklistVo blacklistVo) {
        Blacklist blacklist = new Blacklist();
        BeanUtils.copyProperties(blacklistVo, blacklist);
        String[] list = blacklistMapper.getBlacklistIdStrings(blacklist);
        return blacklistMapper.delByBlacklistIds(list);
    }

    @Override
    public int importBlacklist(List<BlacklistVo> blacklists,String accountName) {

        //调用根据账号得到admin的id的通用方法
        Long userId = userMapper.getUserIdByAccountName (accountName);

        List<Blacklist> blacklistList = new ArrayList<>(500);
        for (BlacklistVo blacklist : blacklists) {
            // 复制属性
            if (blacklist.getClientName() ==null ||blacklist.getClientPhone()==null){
                continue;
            }
            Blacklist blacklist1 = BlacklistConverter.converterToBlacklist(blacklist);
            blacklist1.setUserId(userId);
            blacklist1.setRichClass(1);
            blacklistList.add(blacklist1);
            // 分批处理
            if (blacklistList.size() % 500 == 0) {
                blacklistMapper.importBlacklist(blacklistList);
                blacklistList.clear();
            }
        }
        return blacklistMapper.importBlacklist(blacklistList);
    }
}
